﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Ext.Net;
using InformationSystem.BLL.Core.Service;
using InformationSystem.BLL.Core.Model;
using System.IO;

namespace InformationSystem.WebSite.Module.NoticeMgr
{
    public partial class MessageMgr : BasePage
    {
        public IInformationService InformationService { get; set; }
        protected LoginUser user;
        protected void Page_Load(object sender, EventArgs e)
        {
            user = (LoginUser)Session["LoginUser"];
            if (!X.IsAjaxRequest && !IsPostBack)
            {
                if (user.LoginUserType == UserType.Admin || user.LoginUserType == UserType.Assistant)
                    TreePanel1.ShowNode("sent");
                else
                    TreePanel1.HideNode("sent");

                TreePanel1.SelectNode("recv");
                hdntype.Value = "recv";
                btnDelete.Hidden = true;
                this.LoadRecvd();
            }
        }

        private void LoadRecvd()
        {
            IList<Msg> msglist = InformationService.GetMsgRecvList(user.UserID,0);
            msgStore.DataSource = msglist;
            msgStore.DataBind();
        }
        private void LoadSent()
        {
            IList<Msg> msglist = InformationService.GetMsgSendList(user.UserID);
            msgStore.DataSource = msglist;
            msgStore.DataBind();
        }

        protected void BeforeRecordDeleted(object sender, BeforeRecordDeletedEventArgs e)
        {
            Msg msg = e.Object<Msg>();
            string path = Path.Combine(Server.MapPath("~"), Common.MsgAttachmentPath);
            bool result = this.InformationService.DeleteMsgSent(msg.MsgID,user.UserID,path);
            if (result)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "删除成功！"
                });
            }
            else
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "你没有权限删除该消息！"
                });
            }
        }
        protected void NodeClick(object sender, DirectEventArgs e)
        {
            if (e.ExtraParams.Count == 0) return;
            string otype = e.ExtraParams["sid"];
            if (otype.ToLower() == "recv")
            {
                //load recv data
                btnDelete.Hidden = true;
                hdntype.Value = "recv";
                LoadRecvd();
            }
            else if (otype.ToLower() == "sent")
            {
                //load sent data
                btnDelete.Hidden = false;
                hdntype.Value = "sent";
                LoadSent();
            }

        }       
        protected void AfterStoreChanged(object sender, AfterStoreChangedEventArgs e)
        {
            IList<Msg> msglist = InformationService.GetMsgSendList(user.UserID);
            msgStore.DataSource = msglist;
            msgStore.DataBind();
            this.msgStore.RefreshAfterSaving = RefreshAfterSavingMode.Always;
        }



        protected override bool CommitPermissionCheck()
        {
            throw new NotImplementedException();
        }
    }
}